/**
 * @file       PagingByEditFieldStrategy_test.cpp
 * @brief      TODO
 * @author     Huabo Zheng
 * @date       Feb 2, 2012
 * @Copyright  2012, www.netgear.com Inc. All rights reserved.
 */

#include "PagingByEditFieldStrategy.h"
#include "gtest/gtest.h"
#include "errorcode.h"
#include "PageInfo.h"
#include "Label.h"
#include "typedefine.h"
#include "Button.h"
#include "Frame.h"
#include "Rect.h"
#include "Char.h"
namespace NS_LCD_AFW
{
class CPagingByEditFieldStrategy_test: public ::testing::Test
{
public:
	virtual void SetUp()
	{
		m_pareanRect.setLeft(0);
		m_pareanRect.setTop(0);
		m_pareanRect.setWidth(8);
		m_pareanRect.setHight(4);

		CChar *pChar = NULL;
		for (int i = 0; i < 40; ++i)
		{
			OBJ_NEW(CChar, pChar);
			std::ostringstream os;
			os << i;
			pChar->create(os.str(), CRect(0, 0, 1, 1));
			m_FramePtrArray.push_back(pChar);
		}

	}
	virtual void TearDown()
	{
		CFramePtrArray::iterator iter = m_FramePtrArray.begin();
		for (; iter != m_FramePtrArray.end(); ++iter)
		{
			OBJ_DESTORY((*iter));
		}
		m_FramePtrArray.clear();
	}

protected:
	CFramePtrArray m_FramePtrArray;
	CRect m_pareanRect;
};
TEST_F(CPagingByEditFieldStrategy_test,test_defualtConstruction)
{
}
TEST_F(CPagingByEditFieldStrategy_test,test_calculatePaging)
{
	CFramePtrArray emptyArray;
	emptyArray.clear();
	CPageInfo pi;
	CPagingByEditFieldStrategy *pStrategy = new CPagingByEditFieldStrategy();
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculatePaging(emptyArray,m_pareanRect,pi));
	EXPECT_EQ(1, pi.getTotalPage());
	EXPECT_EQ(1, pi.getCurPage());
	EXPECT_EQ(0, pi.getStartIndex());
	EXPECT_EQ(1, pi.getEndIndex());
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculatePaging(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(2, pi.getTotalPage());
	EXPECT_EQ(1, pi.getCurPage());
	EXPECT_EQ(0, pi.getStartIndex());
	EXPECT_EQ(32, pi.getEndIndex());
	pi.setCurPage(2);
	EXPECT_EQ(2, pi.getCurPage());
	EXPECT_EQ(32, pi.getStartIndex());
	EXPECT_EQ(40, pi.getEndIndex());
	OBJ_DELETE(pStrategy);
}
TEST_F(CPagingByEditFieldStrategy_test,test_findNextHightlightableObjectIndex)
{
	CPageInfo pi;
	CPagingByEditFieldStrategy *pStrategy = new CPagingByEditFieldStrategy();
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculatePaging(m_FramePtrArray,m_pareanRect,pi));
	pi.setCurPage(1);
	EXPECT_EQ(
			31,
			pStrategy->findNextHightlightableObjectIndex(m_FramePtrArray,pi.getEndIndex() - 1,pi.getStartIndex(),false));
	pi.setCurPage(2);
	EXPECT_EQ(
			32,
			pStrategy->findNextHightlightableObjectIndex(m_FramePtrArray,pi.getStartIndex(),pi.getEndIndex()));

	OBJ_DELETE(pStrategy);
}
TEST_F(CPagingByEditFieldStrategy_test,test_calculateViewPort)
{
	CPageInfo pi;
	CPagingByEditFieldStrategy *pStrategy = new CPagingByEditFieldStrategy();
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculatePaging(m_FramePtrArray,m_pareanRect,pi));
	pi.setDirection(CPageInfo::UNKNOW);
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(1, pi.getCurPage());
	EXPECT_TRUE(pi.isNeedSwitchPage());
	EXPECT_EQ(0, pi.getStartIndex());
	EXPECT_EQ(32, pi.getEndIndex());
	EXPECT_EQ(0, pi.getHightLightObjIndex());

	pi.setDirection(CPageInfo::UP);
	EXPECT_EQ(ERR_NO_CHANGE,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(1, pi.getCurPage());
	EXPECT_FALSE(pi.isNeedSwitchPage());
	EXPECT_EQ(0, pi.getStartIndex());
	EXPECT_EQ(32, pi.getEndIndex());
	EXPECT_EQ(0, pi.getHightLightObjIndex());

	pi.setDirection(CPageInfo::DOWN);
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(2, pi.getCurPage());
	EXPECT_TRUE(pi.isNeedSwitchPage());
	EXPECT_EQ(32, pi.getStartIndex());
	EXPECT_EQ(40, pi.getEndIndex());
	EXPECT_EQ(32, pi.getHightLightObjIndex());

	pi.setDirection(CPageInfo::DOWN);
	EXPECT_EQ(ERR_NO_CHANGE,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(2, pi.getCurPage());
	EXPECT_FALSE(pi.isNeedSwitchPage());
	EXPECT_EQ(32, pi.getStartIndex());
	EXPECT_EQ(40, pi.getEndIndex());
	EXPECT_EQ(32, pi.getHightLightObjIndex());

	pi.setDirection(CPageInfo::UP);
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(1, pi.getCurPage());
	EXPECT_TRUE(pi.isNeedSwitchPage());
	EXPECT_EQ(0, pi.getStartIndex());
	EXPECT_EQ(32, pi.getEndIndex());
	EXPECT_EQ(31, pi.getHightLightObjIndex());

	pi.setDirection(CPageInfo::UP);
	EXPECT_EQ(ERR_NO_CHANGE,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(1, pi.getCurPage());
	EXPECT_FALSE(pi.isNeedSwitchPage());
	EXPECT_EQ(0, pi.getStartIndex());
	EXPECT_EQ(32, pi.getEndIndex());
	EXPECT_EQ(31, pi.getHightLightObjIndex());

	pi.setDirection(CPageInfo::RIGHT);
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(2, pi.getCurPage());
	EXPECT_TRUE(pi.isNeedSwitchPage());
	EXPECT_EQ(32, pi.getStartIndex());
	EXPECT_EQ(40, pi.getEndIndex());
	EXPECT_EQ(32, pi.getHightLightObjIndex());

	pi.setDirection(CPageInfo::RIGHT);
	EXPECT_EQ(ERR_NO_CHANGE,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(2, pi.getCurPage());
	EXPECT_FALSE(pi.isNeedSwitchPage());
	EXPECT_EQ(32, pi.getStartIndex());
	EXPECT_EQ(40, pi.getEndIndex());
	EXPECT_EQ(33, pi.getHightLightObjIndex());

	pi.setDirection(CPageInfo::LEFT);
	EXPECT_EQ(ERR_NO_CHANGE,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(2, pi.getCurPage());
	EXPECT_FALSE(pi.isNeedSwitchPage());
	EXPECT_EQ(32, pi.getStartIndex());
	EXPECT_EQ(40, pi.getEndIndex());
	EXPECT_EQ(32, pi.getHightLightObjIndex());

	pi.setDirection(CPageInfo::LEFT);
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(1, pi.getCurPage());
	EXPECT_TRUE(pi.isNeedSwitchPage());
	EXPECT_EQ(0, pi.getStartIndex());
	EXPECT_EQ(32, pi.getEndIndex());
	EXPECT_EQ(31, pi.getHightLightObjIndex());

	pi.setDirection(CPageInfo::LEFT);
	EXPECT_EQ(ERR_NO_CHANGE,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(1, pi.getCurPage());
	EXPECT_FALSE(pi.isNeedSwitchPage());
	EXPECT_EQ(0, pi.getStartIndex());
	EXPECT_EQ(32, pi.getEndIndex());
	EXPECT_EQ(30, pi.getHightLightObjIndex());

	pi.setHightLightObjIndex(0);
	pi.setDirection(CPageInfo::LEFT);
	EXPECT_EQ(ERR_NO_CHANGE,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(1, pi.getCurPage());
	EXPECT_FALSE(pi.isNeedSwitchPage());
	EXPECT_EQ(0, pi.getStartIndex());
	EXPECT_EQ(32, pi.getEndIndex());
	EXPECT_EQ(0, pi.getHightLightObjIndex());

	OBJ_DELETE(pStrategy);
}

} /* namespace NS_LCD_AFW */
